/*
 * @lc app=leetcode.cn id=566 lang=cpp
 *
 * [566] 重塑矩阵
 */

// @lc code=start
class Solution
{
public:
  vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c)
  {
    int m = mat.size(), n = mat[0].size();
    if ((m * n != r * c) || (m == r && n == c))
      return mat;
    vector<vector<int>> v(r, vector<int>(c));
    int row = 0, col = 0;
    for (int i = 0; i < r; ++i) {
      for (int j = 0; j < c; ++j) {
        v[i][j] = mat[row][col];
        if (++col == n) {
          ++row;
          col = 0;
        }
      }
    }
    return v;
  }
};
// @lc code=end
